package cn.iocoder.yudao.module.picturebook.controller.admin.minio;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.minio.FileInfo;
import cn.iocoder.yudao.module.picturebook.service.minio.MinioService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/picture/file")
@Tag(name = "文件上传接口")
 public class MinioController {

    @Resource
    private MinioService minioService;

    @PostMapping("/upload")
    @Operation(summary = "上传文件")

    public CommonResult<FileInfo> uploadFile(@RequestParam("file") MultipartFile file,
                                             @RequestParam(value = "bucketName", required = false) String bucketName) {
        FileInfo fileInfo = minioService.uploadFile(file, bucketName);
        return CommonResult.success( fileInfo);
        //return CommonResult.success("上传成功", fileInfo);
    }

    @PostMapping("/uploadBatch")
    @Operation(summary = "批量上传文件")
    public CommonResult<List<FileInfo>> uploadBatch(@RequestParam("files") MultipartFile[] files,
                                            @RequestParam(value = "bucketName", required = false) String bucketName) {
        List<FileInfo> fileInfos = new java.util.ArrayList<>(files.length);
        for (MultipartFile file : files) {
            FileInfo fileInfo = minioService.uploadFile(file, bucketName);
            fileInfos.add(fileInfo);
        }
        return CommonResult.success(fileInfos);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文件")
    public CommonResult<String> deleteFile(@RequestParam("bucketName") String bucketName,
                                           @RequestParam("objectName") String objectName) {
        minioService.removeObject(bucketName, objectName);
        return CommonResult.success("删除成功");
    }

    @GetMapping("/info")
    @Operation(summary = "获取文件信息")
    public CommonResult<FileInfo> getFileInfo(@RequestParam("bucketName") String bucketName,
                                        @RequestParam("objectName") String objectName) {
        FileInfo fileInfo = minioService.getFileInfo(bucketName, objectName);
        return CommonResult.success( fileInfo);
    }

    @GetMapping("/presignedUrl")
    @Operation(summary = "获取文件预签名URL")
    public CommonResult<String> getPresignedUrl(@RequestParam("bucketName") String bucketName,
                                          @RequestParam("objectName") String objectName,
                                          @RequestParam(value = "expires", required = false) Integer expires) {
        String url = minioService.getPresignedObjectUrl(bucketName, objectName, expires);
        return CommonResult.success(url);
    }

    @GetMapping("/buckets")
    @Operation(summary = "获取所有存储桶")
    public CommonResult<List<String>> getAllBuckets() {
        List<String> buckets = minioService.getAllBuckets();
        return CommonResult.success( buckets);
    }
}